package us.icitap.cm.ui.custom;

import us.icitap.cm.listeners.ReturnObjectListener;

import com.google.code.geocoder.Geocoder;
import com.google.code.geocoder.GeocoderRequestBuilder;
import com.google.code.geocoder.model.GeocodeResponse;
import com.google.code.geocoder.model.GeocoderComponent;
import com.google.code.geocoder.model.GeocoderRequest;
import com.google.code.geocoder.model.GeocoderResult;
import com.google.code.geocoder.model.GeocoderStatus;
import com.vaadin.data.Property.ValueChangeEvent;
import com.vaadin.data.Property.ValueChangeListener;
import com.vaadin.data.util.BeanItemContainer;
import com.vaadin.event.ShortcutAction.KeyCode;
import com.vaadin.ui.Alignment;
import com.vaadin.ui.Button.ClickEvent;
import com.vaadin.ui.Button.ClickListener;
import com.vaadin.ui.CustomComponent;
import com.vaadin.ui.FormLayout;
import com.vaadin.ui.ListSelect;
import com.vaadin.ui.AbstractSelect.ItemCaptionMode;
import com.vaadin.ui.TextField;
import com.vaadin.ui.themes.BaseTheme;

@SuppressWarnings("serial")
public class SearchAddressComponent extends CustomComponent {
	private final CustVerticalLayout layout = new CustVerticalLayout();
	
	private final TextField addressField = new TextField();
	private final CustButton searchBtn = new CustButton("Kerko");
	private final CustButton switchBtn = new CustButton("");
	private final ListSelect listSelect = new ListSelect();
	private final BeanItemContainer<GeocoderResult> container = new BeanItemContainer<GeocoderResult>(GeocoderResult.class);
	private final Geocoder geocoder = new Geocoder();
	
	private final CustTextField latField = new CustTextField("Latitude");
	private final CustTextField lonField = new CustTextField("Longitude");
	private boolean searchByAddress = true;

	private ReturnObjectListener listener;
	public SearchAddressComponent(boolean _searchByAddress, String width){
		layout.setWidth(width);
		layout.setHeight("100%");
		layout.setMargin(true);
		setWidth(width);
		setHeight("100%");
		
		addressField.setImmediate(true);
		addressField.setSizeFull();
		addressField.setWidth("250px");
		searchBtn.setWidth("100px");
		searchBtn.setClickShortcut(KeyCode.ENTER);
		searchBtn.addClickListener(new ClickListener(){
			@Override
			public void buttonClick(ClickEvent event) {
				if(searchByAddress){
					
				}
				
				String adr = addressField.getValue();
				if(adr==null || "".equals(adr))return;
				
				GeocoderRequest geocoderRequest = new GeocoderRequestBuilder()
								.setAddress(adr)
								.setLanguage("SQ")
								.addComponent(GeocoderComponent.COUNTRY, "AL")
								.getGeocoderRequest();
				GeocodeResponse geocoderResponse = geocoder.geocode(geocoderRequest);
				
				container.removeAllItems();
				if(geocoderResponse.getStatus()==GeocoderStatus.ZERO_RESULTS)
					return;
				if(geocoderResponse.getStatus()==GeocoderStatus.OK){
					if(geocoderResponse.getResults().size()==1){
						listSelect.setVisible(false);
						addressField.setValue(geocoderResponse.getResults().get(0).getFormattedAddress());
						listener.returnObject(geocoderResponse.getResults().get(0));
					} else {
						listSelect.setVisible(true);
						container.addAll(geocoderResponse.getResults());						
					}
				}
			}			
		});
		
		
		listSelect.setSizeFull();
		listSelect.setImmediate(true);
		listSelect.setNullSelectionAllowed(false);
		listSelect.setContainerDataSource(container);
		listSelect.setItemCaptionMode(ItemCaptionMode.EXPLICIT);
		listSelect.setItemCaptionPropertyId("formattedAddress");
		listSelect.setRows(4);
//		listSelect.setVisible(false);
		listSelect.addValueChangeListener(new ValueChangeListener(){
			@Override
			public void valueChange(ValueChangeEvent event) {
				GeocoderResult gcr = (GeocoderResult) event.getProperty().getValue();
				listSelect.setVisible(false);
				addressField.setValue(gcr.getFormattedAddress());
				listener.returnObject(gcr);
			}
		});
		
		final CustHorizontalLayout chl = new CustHorizontalLayout();
		chl.setSizeFull();
		chl.addComponentAsFirst(addressField);
		
		final FormLayout fl = new FormLayout();
		fl.setMargin(false);
		fl.addComponent(latField);
		fl.addComponent(lonField);
		
		chl.addComponent(fl);
		chl.addComponent(searchBtn);
		chl.setComponentAlignment(searchBtn, Alignment.BOTTOM_CENTER);
		switchBtn.setStyleName(BaseTheme.BUTTON_LINK);
		switchBtn.addClickListener(new ClickListener(){
			@Override
			public void buttonClick(ClickEvent event) {
				// TODO Auto-generated method stub
				setSearchByAddress(!searchByAddress);
				chl.setExpandRatio(searchByAddress?addressField:fl, 1);
			}
			
		});
		chl.addComponent(switchBtn);
		chl.setComponentAlignment(switchBtn, Alignment.BOTTOM_CENTER);
		layout.addComponent(chl);
		layout.addComponent(listSelect);
		
		setSearchByAddress(_searchByAddress);
		chl.setExpandRatio(searchByAddress?addressField:fl, 1);
			
		setCompositionRoot(layout);		
	}
	
	private void setSearchByAddress(boolean _searchByAddress){
		this.searchByAddress = _searchByAddress;
		addressField.setVisible(_searchByAddress);
		listSelect.setVisible(false);
		latField.setVisible(!_searchByAddress);
		lonField.setVisible(!_searchByAddress);
		switchBtn.setCaption(_searchByAddress?"Koordinata":"Adresa");
		
	}
		
	
	
	public void addListener(ReturnObjectListener listener){
		this.listener = listener;
	}
}
